 package tools;

 import java.lang.management.LockInfo;
 import java.lang.management.ManagementFactory;
 import java.lang.management.MonitorInfo;
 import java.lang.management.ThreadInfo;
 import java.lang.management.ThreadMXBean;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;




 public class DeadlockDetector
   implements Runnable
 {
/*  17 */   private static final Logger log = LogManager.getLogger(DeadlockDetector.class);
   private static final String INDENT = "    ";
/*  19 */   private int checkInterval = 0;
/*  20 */   private StringBuilder sb = null;

   public DeadlockDetector(int checkInterval) {
/*  23 */     this.checkInterval = checkInterval * 1000;
   }


   public void run() {
/*  28 */     boolean noDeadLocks = true;

/*  30 */     while (noDeadLocks) {
       try {
/*  32 */         ThreadMXBean bean = ManagementFactory.getThreadMXBean();
/*  33 */         long[] threadIds = bean.findDeadlockedThreads();

/*  35 */         if (threadIds != null) {
/*  36 */           log.error("Deadlock detected!");
/*  37 */           this.sb = new StringBuilder();
/*  38 */           noDeadLocks = false;

/*  40 */           ThreadInfo[] infos = bean.getThreadInfo(threadIds);
/*  41 */           this.sb.append("\nTHREAD LOCK INFO: \n");
/*  42 */           for (ThreadInfo threadInfo : infos) {
/*  43 */             printThreadInfo(threadInfo);
/*  44 */             LockInfo[] lockInfos = threadInfo.getLockedSynchronizers();
/*  45 */             MonitorInfo[] monitorInfos = threadInfo.getLockedMonitors();

/*  47 */             printLockInfo(lockInfos);
/*  48 */             printMonitorInfo(threadInfo, monitorInfos);
           }

/*  51 */           this.sb.append("\nTHREAD DUMPS: \n");
/*  52 */           for (ThreadInfo ti : bean.dumpAllThreads(true, true)) {
/*  53 */             printThreadInfo(ti);
           }
/*  55 */           log.error(this.sb.toString());
         }
/*  57 */         Thread.sleep(this.checkInterval);
/*  58 */       } catch (Exception ex) {
/*  59 */         ex.printStackTrace();
       }
     }
   }

   private void printThreadInfo(ThreadInfo threadInfo) {
/*  65 */     printThread(threadInfo);
/*  66 */     this.sb.append("    ").append(threadInfo.toString()).append("\n");
/*  67 */     StackTraceElement[] stacktrace = threadInfo.getStackTrace();
/*  68 */     MonitorInfo[] monitors = threadInfo.getLockedMonitors();

/*  70 */     for (int i = 0; i < stacktrace.length; i++) {
/*  71 */       StackTraceElement ste = stacktrace[i];
/*  72 */       this.sb.append("    ").append("at ").append(ste.toString()).append("\n");
/*  73 */       for (MonitorInfo mi : monitors) {
/*  74 */         if (mi.getLockedStackDepth() == i) {
/*  75 */           this.sb.append("    ").append("  - locked ").append(mi).append("\n");
         }
       }
     }
   }

   private void printThread(ThreadInfo ti) {
/*  82 */     this.sb.append("\nPrintThread\n");
/*  83 */     this.sb.append("\"").append(ti.getThreadName()).append("\" Id=").append(ti.getThreadId()).append(" in ").append(ti.getThreadState()).append("\n");
/*  84 */     if (ti.getLockName() != null) {
/*  85 */       this.sb.append(" on lock=").append(ti.getLockName()).append("\n");
     }
/*  87 */     if (ti.isSuspended()) {
/*  88 */       this.sb.append(" (suspended)\n");
     }
/*  90 */     if (ti.isInNative()) {
/*  91 */       this.sb.append(" (running in native)\n");
     }
/*  93 */     if (ti.getLockOwnerName() != null) {
/*  94 */       this.sb.append("    ").append(" owned by ").append(ti.getLockOwnerName()).append(" Id=").append(ti.getLockOwnerId()).append("\n");
     }
   }

   private void printMonitorInfo(ThreadInfo threadInfo, MonitorInfo[] monitorInfos) {
/*  99 */     this.sb.append("    ").append("Locked monitors: count = ").append(monitorInfos.length).append("\n");
/* 100 */     for (MonitorInfo monitorInfo : monitorInfos) {
/* 101 */       this.sb.append("    ").append("  - ").append(monitorInfo).append(" locked at \n");
/* 102 */       this.sb.append("    ").append("      ").append(monitorInfo.getLockedStackDepth()).append(" ").append(monitorInfo.getLockedStackFrame()).append("\n");
     }
   }

   private void printLockInfo(LockInfo[] lockInfos) {
/* 107 */     this.sb.append("    ").append("Locked synchronizers: count = ").append(lockInfos.length).append("\n");
/* 108 */     for (LockInfo lockInfo : lockInfos)
/* 109 */       this.sb.append("    ").append("  - ").append(lockInfo).append("\n"); 
   }
 }


/* Location:              C:\PGX\Server.jar!\tools\DeadlockDetector.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */